<?php
/**
 * User: bgl
 * Date: 2023/6/13
 * Time: 16:54
 */

namespace App\Admin\Controllers\Products;

use App\Admin\Extensions\Grid\Tool\Import;
use App\Models\Product\ProductSkuColor;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class SkuColorController extends AdminController{

    protected $title = '基础颜色';

    protected function grid(){

        $grid = new Grid(new ProductSkuColor());
        $grid->column('id', 'ID');
        $grid->column('color_code', '颜色编码');
        $grid->column('color_cn', '颜色名称(中文)');
        $grid->column('color_en', '颜色名称(英文)');
        $grid->column('remar', '备注');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function (Grid\Tools $tools) {
            //$tools->append(new ImportSkuColor());
            $tools->append(new Import('products/sku-color-upload'));
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->where('color_cn', 'like', "%{$this->input}%")
                        ->orWhere('color_en', 'like', "%{$this->input}%")
                        ->orWhere('color_code', 'like', "%{$this->input}%");
                }, '查询','color8')->placeholder('模糊查询颜色名称，编码');
            });

        });

        return $grid;
    }

    public function form(){

        $form = new Form(new ProductSkuColor());

        $form->hidden('id');
        $form->text('color_cn', '颜色名称(中文)')->rules('required')
            ->help('输入颜色中文，例：黑色');
        $form->text('color_en', '颜色名称(英文)')->rules('required')
            ->help('输入颜色英文，例：Black');
        $form->text('color_code', '颜色编码')->pattern('[A-Z]{2}')->rules('required')
            ->help('颜色英文两位大写字母组合，例：黑色(Black)，可标识为：BK');
        $form->textarea('remark', '备注');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }

}
